

#import "EditController.h"
#import "Imports.h"

@implementation EditController

- Cut:sender
{
    	[[TheFileController CurrentSoundView:self] cut:self];
	[TheFileController SoundChanged: [TheFileController CurrentSound:self]];
	return self;
}

- Delete:sender
{
	[[TheFileController CurrentSoundView:self] delete:self];
	    [TheFileController SoundChanged: [TheFileController CurrentSound:self]];
	return self;
}

- SelectAll:sender
{
    [[TheFileController CurrentSoundView:self] selectAll:self];
	return self;
}

- ConvertPasteBoard:sender
{
	[ConvertPasteBoardPanel  makeKeyAndOrderFront:self];
	[NXApp runModalFor:ConvertPasteBoardPanel];
	return self;
}

- CancelConvertPasteBoard:sender;
{
	[NXApp stopModal];
	[ConvertPasteBoardPanel close];
	return self;
}

- OkayConvertPasteBoard:sender;
{
	id 		TheClipSound;
	int 	TheFormat;
	double 	TheRate;
	int		TheCount;
	[NXApp stopModal];
	[ConvertPasteBoardPanel close];
	TheClipSound=[Sound newFromPasteboard];
	if ([MuLawButton intValue])
		{
		TheFormat=SND_FORMAT_MULAW_8;
		TheRate=SND_RATE_CODEC;
		}
	else if ([FortyButton intValue])
		{
		TheFormat=SND_FORMAT_LINEAR_16;
		TheRate=SND_RATE_HIGH;
		}
	else
		{
		TheFormat=SND_FORMAT_LINEAR_16;
		TheRate=SND_RATE_LOW;
		}
	
	if ([MonoButton intValue])
		{TheCount=1;}
	else {TheCount=2;}
		
	if (TheClipSound!=nil)
		{
		[TheClipSound convertToFormat:TheFormat
						 samplingRate:TheRate
						 channelCount:TheCount];
		[TheClipSound writeToPasteboard];
		[TheClipSound free];
		}
	return self;
}
- PasteInto:sender
{
   [[TheFileController CurrentSoundView:self] paste:self];
 [TheFileController SoundChanged: [TheFileController CurrentSound:self]];
    	return self;
}

- Copy:sender
{
    [[TheFileController CurrentSoundView:self] copy:self];
	return self;
}

- ZoomToSelection:sender
{
	NXRect BoundsRect;
	NXRect VisibleRect;
	float ScreenWidth;
	float NewReductionFactor;
	NXPoint NewPoint;
	int TotalNumberOfSamples;
	int NumberOfSamples;
	int FirstSample;
	int x;
	
	[[TheFileController CurrentWindow:self] disableFlushWindow];
	for (x=1;x<=2;x++)				// need to do twice sometimes.
		{
		TotalNumberOfSamples=[[TheFileController CurrentSound:self] sampleCount];
		[[TheFileController CurrentSoundView:self] getSelection:&FirstSample size:&NumberOfSamples];
 		[[TheFileController CurrentSoundView:self] getVisibleRect: &VisibleRect];
	
		//Step One:  Zoom to the right reduction

		ScreenWidth=VisibleRect.size.width;
		NewReductionFactor=((float)NumberOfSamples)/ScreenWidth;
		[[TheFileController CurrentSoundView:self] setReductionFactor: NewReductionFactor];
		
		// Step Two:  Move to the right spot
	
		[[[TheFileController CurrentSoundView:self] superview] getDocRect: &BoundsRect];
	
		NewPoint.x=BoundsRect.size.width * (float) FirstSample / (float) TotalNumberOfSamples+250;
				// no idea why 250 adjusts properly... sorry for the magic number.
	
		[[[TheFileController CurrentSoundView:self] superview] _scrollTo: &NewPoint];
				// THIS IS A PRIVATE METHOD.  IT WILL COMPILE AS "Cannot Find Method..."
				// IGNORE THIS MESSAGE.
				
		[[[TheFileController CurrentWindow:self] contentView] reflectScroll: 
				[[TheFileController CurrentSoundView:self] superview]];
		[[TheFileController CurrentSoundView:self] setSelection:FirstSample size:NumberOfSamples];
		[self ZoomChanged:self];
		}
	[[TheFileController CurrentWindow:self] reenableFlushWindow];
	[[TheFileController CurrentWindow:self] display];
	return self;
}
- ZoomSmallIn:sender;
{
	[self ZoomBy:17.5];
	return self;
}


- ZoomLargeIn:sender;
{
	[self ZoomBy:2.5];
	return self;
}


- ZoomSmallOut:sender;
{
	[self ZoomBy:-17.5];
	return self;
}


- ZoomLargeOut:sender;
{
	[self ZoomBy:-2.5];	
	return self;
}


- ZoomOut:sender
{
	[self ZoomBy:-10.0];
	return self;
}

- ZoomIn:sender
{
	[self ZoomBy:10.0];
	return self;
}

- ZoomBy: (float) ThisZoomFactor
{
	int FirstSample, SampleCount;
	NXSize ScrollSize;
	float NumberOfSamples=(float)[[TheFileController CurrentSound:self]  sampleCount];
	float CurrentReductionFactor=
		[[TheFileController CurrentSoundView:self] reductionFactor];
	float MaximumReductionFactor;
	float ZoomLimit;
	float ZoomReductionFactor;
	
	[[[TheFileController CurrentWindow:self] contentView] getContentSize:&ScrollSize];
	MaximumReductionFactor= NumberOfSamples/ScrollSize.width;
	[[TheFileController CurrentSoundView:self] getSelection: &FirstSample
											        size: &SampleCount];
	

	if (ThisZoomFactor<0)
		{
		ZoomLimit=0-(MaximumReductionFactor+MaximumReductionFactor/ThisZoomFactor);
		ZoomReductionFactor=0-CurrentReductionFactor;
		}
	else
		{
		ZoomLimit=1+MaximumReductionFactor/ThisZoomFactor;
		ZoomReductionFactor=CurrentReductionFactor;
		}
	
	if (ZoomReductionFactor>=ZoomLimit)
	{
	[[TheFileController CurrentSoundView:self] setReductionFactor:
		(CurrentReductionFactor-MaximumReductionFactor/ThisZoomFactor)];
	}
	else 
		{
		if (ThisZoomFactor<0)
			{
			[self ZoomAllOut:self];
			}
		else
			{
			[self ZoomAllIn:self];
			}
		}
	[[TheFileController CurrentSoundView:self] display];
	[[TheFileController CurrentSoundView:self] setSelection:FirstSample
												    size:SampleCount];
	[self ZoomChanged:self];
	return self;

}

- ZoomAllIn:sender
{
	int FirstSample, SampleCount;
	[[TheFileController CurrentSoundView:self] getSelection: &FirstSample
											        size: &SampleCount];
	
	[[TheFileController CurrentSoundView:self] setReductionFactor: 1];
	[[TheFileController CurrentSoundView:self] display];
	[[TheFileController CurrentSoundView:self] setSelection:FirstSample
												    size:SampleCount];
	[self ZoomChanged:self];
	return self;
}

- ZoomAllOut:sender
{
	int FirstSample, SampleCount;
	NXSize ScrollSize;
	float NumberOfSamples=(float)[[TheFileController CurrentSound:self]  sampleCount];
	[[TheFileController CurrentSoundView:self] getSelection: &FirstSample
											        size: &SampleCount];
	
	[[[TheFileController CurrentWindow:self] contentView] getContentSize:&ScrollSize];
	[[TheFileController CurrentSoundView:self] setReductionFactor:
			NumberOfSamples / ScrollSize.width];
	[[TheFileController CurrentSoundView:self] display];
	[[TheFileController CurrentSoundView:self] setSelection:FirstSample
												    size:SampleCount];
	[self ZoomChanged:self];
	return self;
}

- ChangeZoom:sender
{
	int FirstSample, SampleCount;
	NXSize ScrollSize;
	float NumberOfSamples=(float)[[TheFileController CurrentSound:self]  sampleCount];
	float MaximumReductionFactor;
	
	[[[TheFileController CurrentWindow:self] contentView] getContentSize:&ScrollSize];
	MaximumReductionFactor= NumberOfSamples/ScrollSize.width;
	[[TheFileController CurrentSoundView:self] getSelection: &FirstSample
											        size: &SampleCount];
	
	
	if ([TheFileController CurrentSoundView:self]!=NULL)
	{[[TheFileController CurrentSoundView:self] setReductionFactor: 
		([sender floatValue]*(MaximumReductionFactor-1)+1)];
	}
	[[TheFileController CurrentSoundView:self] setSelection:FirstSample size:SampleCount];
	[self ZoomChanged:self];
	return self;
}

- ZoomChanged:sender
{
	NXSize ScrollSize;
	float NumberOfSamples=(float)[[TheFileController CurrentSound:self]  sampleCount];
	float MaximumReductionFactor;
	
	[[[TheFileController CurrentWindow:self] contentView] getContentSize:&ScrollSize];
	MaximumReductionFactor= NumberOfSamples/ScrollSize.width;
	
	
	if (([TheFileController CurrentSoundView:self]!=NULL)&&([ZoomWindow isVisible]))
		{[ZoomSlider setFloatValue:
			([[TheFileController CurrentSoundView:self] reductionFactor]-1) /
			(MaximumReductionFactor-1)];
		}
	[self ChangeSelection:self];
	[ZoomField setFloatValue: [ZoomSlider floatValue]];
	return self;
}

- TurnOnMenu:sender
{
	[self ChangeSelection:self];
	[EditButton setEnabled:YES];
	[ZoomButton setEnabled:YES];
	return self;
}

- TurnOffMenu:sender
{
	[self ResetSelection:self];
	[EditButton setEnabled:NO];
	[ZoomButton setEnabled:NO];
	return self;
}

- ChangeSelection:sender
{
	int FirstSample, SampleCount;
	float SamplingRate;
	
	SamplingRate = (float)[[TheFileController CurrentSound:self] samplingRate];
	[[TheFileController CurrentSoundView:self] getSelection: &FirstSample
												   size: &SampleCount];
	[SelectionStart setIntValue: FirstSample];
	[SelectionLength setIntValue: SampleCount];
	[SelectionEnd setIntValue: FirstSample+SampleCount];
	[SelectionSecondsStart setFloatValue: (float)FirstSample /SamplingRate];
	[SelectionSecondsLength setFloatValue: (float)SampleCount /SamplingRate];
	[SelectionSecondsEnd setFloatValue: (float)(FirstSample+SampleCount) /SamplingRate];
	return self;
}


- ChangeView:sender
{	
	NXRect VisibleRect;
	float ReductionFactor;
	int FirstViewSample, ViewSampleCount;
	float SamplingRate;
	
	SamplingRate = (float)[[TheFileController CurrentSound:self] samplingRate];
	[[TheFileController CurrentSoundView:self] getVisibleRect: &VisibleRect];
	ReductionFactor=[[TheFileController CurrentSoundView:self] reductionFactor];
	FirstViewSample=(int) (VisibleRect.origin.x*ReductionFactor);
	ViewSampleCount=(int) (VisibleRect.size.width*ReductionFactor);
	[ViewStart setIntValue: FirstViewSample];
	[ViewLength setIntValue: ViewSampleCount];
	[ViewEnd setIntValue: FirstViewSample+ViewSampleCount];
	[ViewSecondsStart setFloatValue: (float)FirstViewSample /SamplingRate];
	[ViewSecondsLength setFloatValue: (float)ViewSampleCount /SamplingRate];
	[ViewSecondsEnd setFloatValue: (float)(FirstViewSample+ViewSampleCount) /SamplingRate];

	
	return self;
}

- ResetSelection:sender
{
	[SelectionStart setIntValue: 0];
	[SelectionLength setIntValue: 0];
	[SelectionEnd setIntValue: 0];
	[SelectionSecondsStart setIntValue:0];
	[SelectionSecondsLength setIntValue:0];
	[SelectionSecondsEnd setIntValue:0];
	
	[ViewStart setIntValue: 0];
	[ViewLength setIntValue: 0];
	[ViewEnd setIntValue: 0];
	[ViewSecondsStart setIntValue:0];
	[ViewSecondsLength setIntValue:0];
	[ViewSecondsEnd setIntValue:0];
	
	return self;

}

- CompactSound:sender
{
	[[TheFileController CurrentSound:self] compactSamples];
	[TheFileController SoundChanged: [TheFileController CurrentSound:self]];
	return self;
}

- SetContinuousZoom: (BOOL) ZoomOn;
{
	[ZoomSlider setContinuous: ZoomOn];
	return self;
}


/*  IMPLEMENTED BY EDITCONTROLLER AS A DELEGATE OF PANELS */

- windowDidBecomeMain:sender
{
	if (sender==ZoomWindow)
		{[self ZoomChanged:self];}
	return self;
}

- windowDidBecomeKey:sender
{	
	if (sender==ZoomWindow)
		{[self ZoomChanged:self];}
	return self;
}

- windowDidDeminiaturize:sender
{		
	if (sender==ZoomWindow)
		{[self ZoomChanged:self];}
	return self;
}


@end
